package ui.activity.login.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import Bean.RegisterBean;
import Bean.RegisterParamBean;
import Bean.SendPhoneCodeBean;
import Bean.SendPhoneCodeParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.login.biz.RegisterBiz;
import ui.activity.login.contract.RegisterContract;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class RegisterPresenter implements RegisterContract.Presenter {

    RegisterContract.View view;
    RegisterBiz registerBiz;
    String mCode = "";
    String mPhone = "";


    @Inject
    public RegisterPresenter(RegisterContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        registerBiz = (RegisterBiz) biz;
    }


    @Override
    public void getRegisterInfo(String phone, String passWord, String rePassWord, String realName, boolean isCheck, String code) {

        if (!isCheck) {
            view.showMsg("请勾选并同意条款");
            return;
        }
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(passWord) || StringUtils.isEmpty(rePassWord) || StringUtils.isEmpty(realName)) {
            view.showMsg("请填写完善注册信息");
            return;
        }

        if (!mPhone.equals(phone)) {
            view.showMsg("手机号与获取验证码手机号不一致");
            return;
        }
        if (!mCode.equals(code)) {
            view.showMsg("验证码不正确");
            return;
        }
        if (!passWord.equals(rePassWord)) {
            view.showMsg("两次填写的密码不一致");
            return;
        }
        view.loading(true);
        RegisterParamBean registerParamBean = new RegisterParamBean();
        registerParamBean.setUserName(phone);
        registerParamBean.setRealName(realName);
        registerParamBean.setPassWord(passWord);
        registerParamBean.setRePassWord(rePassWord);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(registerParamBean));
        registerBiz.getRegisterInfo(requestBody, new BaseBiz.Callback<RegisterBean.DataBean>() {
            @Override
            public void onSuccess(RegisterBean.DataBean info) {
                view.showMsg("注册成功");
                view.loading(false);
                view.finishAct();
            }

            @Override
            public void onFailure(RegisterBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getPhoneCode(String phone) {
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请填写手机号码");
            return;
        }
        view.loading(true);
        SendPhoneCodeParamBean sendPhoneCodeParamBean = new SendPhoneCodeParamBean();
        sendPhoneCodeParamBean.setTelephoneNumber(phone);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(sendPhoneCodeParamBean));
        registerBiz.getPhoneCode(requestBody, new BaseBiz.Callback<SendPhoneCodeBean.DataBean>() {
            @Override
            public void onSuccess(SendPhoneCodeBean.DataBean info) {
                view.showMsg("获取验证码成功");
                mCode = info.getCode();
                mPhone = phone;
                view.upDateUI();
                view.loading(false);
            }

            @Override
            public void onFailure(SendPhoneCodeBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toRegisterWord() {

    }
}
